<template>
  <div class="login-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ title }}</span>
        <el-button-group style="float: right;">
          <el-button :type="activeTab === 'student' ? 'primary' : 'default'" @click="switchTab('student')">
            <UserFilled />
            学生
          </el-button>
          <el-button :type="activeTab === 'teacher' ? 'primary' : 'default'" @click="switchTab('teacher')">
            <User />
            老师
          </el-button>
          <el-button :type="activeTab === 'admin' ? 'primary' : 'default'" @click="switchTab('admin')">
            <Lock />
            管理员
          </el-button>
        </el-button-group>
      </div>
      <el-form :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.username" prefix-icon="User"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password" show-password prefix-icon="Lock"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { ref, computed } from 'vue';
import { ElMessage } from 'element-plus';
import { UserFilled, User, Lock } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';

export default {
  components: {
    UserFilled,
    User,
    Lock
  },
  setup() {
    const activeTab = ref('student');
    const form = ref({
      username: '',
      password: ''
    });

    const router = useRouter(); // 获取router实例

    const title = computed(() => {
      switch (activeTab.value) {
        case 'student':
          return '学生登录';
        case 'teacher':
          return '老师登录';
        case 'admin':
          return '管理员登录';
        default:
          return '登录';
      }
    });

    const switchTab = (tab) => {
      activeTab.value = tab;
    };

    const onSubmit = () => {
      if (!form.value.username || !form.value.password) {
        ElMessage.error('请输入用户名和密码');
        return;
      }
      if (form.value.username.length < 2 || form.value.username.length > 20) {
        ElMessage.error('用户名长度必须在3到20个字符之间');
        return;
      }

      const user = { username: form.value.username, password: form.value.password, type: activeTab.value };
      console.log(user);
      // 这里可以添加登录逻辑
      console.log('登录信息：', user);
      ElMessage.success('登录成功');
      // 让username保存到sessionStorage中
      sessionStorage.setItem('username', user.username);
      sessionStorage.setItem('type', activeTab.value);
      router.push('/index'); // 使用router实例进行页面跳转
    };

    return {
      activeTab,
      form,
      title,
      switchTab,
      onSubmit
    };
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-size: cover;
}

.box-card {
  width: 400px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 10px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.el-form-item__label {
  font-weight: bold;
}

.el-button-group .el-button {
  margin-left: 5px;
}

.el-button-group .el-button:first-child {
  margin-left: 0;
}
</style>